import { useUserStore } from '@/stores/user'
import type { Directive, DirectiveBinding } from 'vue'

// 超级管理员权限
const adminPerm = '*:*:*'

const hasPerm = (el: HTMLElement, binding: DirectiveBinding<string>) => {
  const store = useUserStore()
  const permissions = store.profile.permissions
  
  // 如果是超级管理员，则直接放行
  if (permissions.includes(adminPerm)) {
    return
  }
  // 判断当前组件是否拥有该指令绑定的权限
  if (!permissions.includes(binding.value)) {
    el.remove()
  }
}

export const auth: Directive = {
  mounted: hasPerm,
  updated: hasPerm
}
